<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/19
 * Time: 15:53
 */

namespace app\event;


use app\common\lib\Common;
use bootstrap\DBListen;
use support\Db;

class ConsoleEvent
{
    public function terminate($data)
    {
        list($cmd,$result,$ttls) = $data;
        //var_dump($cmd,$result,$ttls);

        //todo 1 记录SQL语句
        if(!empty($this->sqllogDebug)){
            $content = '['.$cmd.'] '."\n";
            foreach($this->sqllogDebug as $log){
                if(strpos($log,'select 1')===false){
                    $content .= $log."\n";
                }
            }

            //用完就要释放数据，要不出现内存泄漏
            $this->sqllogDebug = [];
            Common::createAndSaveLog($content."\n------------------------------------------------------------\n\n",'console_sql');
        }

        $timedTask = require_once(runtime_path('crontab.php'));

        $key = str_replace([':',' ','  ','-'],'_',trim(strtolower($cmd)));
        //var_dump($key,$timedTask[$key]);

        $result = [
            'data'=>$result,
            'msg'=>'success',
        ];
        isset($timedTask[$key]) && \app\model\cron\CronTaskLog::getInstance()->insertTaskLog($timedTask[$key],$result,$ttls);

    }
}